package app.mybatis.type_handler;

import net.postgis.jdbc.jts.JtsGeometry;
import org.apache.ibatis.type.MappedTypes;
import org.locationtech.jts.geom.Geometry;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author somebody
 */
@MappedTypes(Geometry.class)
public class GeometryTypeHandler extends AbstractGeometryTypeHandler<Geometry> {

    @Override
    public Geometry getNullableResult(ResultSet rs, String columnName) throws SQLException {
        JtsGeometry geom = (JtsGeometry) rs.getObject(columnName);
        if (null == geom) {
            return null;
        }
        return geom.getGeometry();
    }

    @Override
    public Geometry getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        JtsGeometry geom = (JtsGeometry) rs.getObject(columnIndex);
        if (null == geom) {
            return null;
        }
        return geom.getGeometry();
    }

    @Override
    public Geometry getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        JtsGeometry geom = (JtsGeometry) cs.getObject(columnIndex);
        if (null == geom) {
            return null;
        }
        return geom.getGeometry();
    }
}
